import {readdir} from "node:fs/promises";
import cp from 'child_process'
import {db} from "@/lib/db";
import {compare} from "bcrypt";

const execGetSec = async (pathFile) => {
    const cmd = `ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 ${pathFile}`
    console.log('当前指令:', cmd)
    await cp.exec(cmd, (err, stdout, errout) => {
        if (!err) {
            console.log('结果:', stdout)
        }
    })
}

export const execJpg = async (pathFile, saveFilePath) => {
    const cmd = `ffmpeg -i ${pathFile} -y -f image2 -ss 2 -frames 1 ${saveFilePath}`
    console.log('当前指令:', cmd)
    await cp.exec(cmd, (err, stdout, errout) => {
        if (!err) {
            console.log(`${saveFilePath} success...`)
        }
    })
}

// const path = "F:\\codes\\projs\\bilibili\\bili-web\\public\\videos\\1.mp4"
// const imgpath = "F:\\codes\\projs\\bilibili\\bili-web\\public\\videos\\cover.jpg"


export async function createVideo({userId, name, cover}) {
    return await db.video.create({
        data: {userId, name, cover}
    })
}

export async function getVideoList() {
    const files = await readdir('./public/videos')
    return files.filter((f) => f.endsWith('.mp4'))
        .map((f) => f.slice(0, -'.mp4'.length))
}

export async function getVideoListDB() {
    return await db.video.findMany()
}

export async function getVideoById(id) {
    let res = null
    try {
        res = await db.video.findUnique({where: {id}})
    } catch (e) {

    }
    return res
}